package com.portal.back.service.impl;

/**
 * @Title:后台内容service
 * @Copyright: Copyright (c) 0.1
 * @author:鲁根成
 * @version: 1.0
 * @time: 2011-4-7
 * 
 */
import java.util.ArrayList;
import java.util.List;

import com.portal.back.service.ISysContentService;
import com.portal.base.dao.BaseDao;
import com.portal.base.dao.IBaseDao;
import com.portal.domain.SysContent;

import com.portal.util.common.DateTimeUtil;

public class SysContentService implements ISysContentService {
	private IBaseDao<SysContent, Integer> dao;

	public SysContentService() {
		dao = new BaseDao<SysContent, Integer>(SysContent.class);
	}

	public boolean addOrEditCompany(SysContent syscontent) {
		String sql = "";
		try {
			if (syscontent.getId() == null) {
				sql = "insert into sys_content(title,code,content) values(?,?,?)";
				dao.insert(sql, new Object[] { syscontent.getTitle(),
						syscontent.getCode(), syscontent.getContent() });
			} else {
				sql = "update sys_content  set content=? where code='1'";
				dao.update(sql, new Object[] { syscontent.getContent() });
			}

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	public boolean addOrEditNew(SysContent syscontent) {
		String sql = "";
		try {
			if (syscontent.getId() == null) {
				sql = "insert into sys_content(title,code,content) values(?,?,?)";
				dao.insert(sql, new Object[] { syscontent.getTitle(),
						syscontent.getCode(), syscontent.getContent() });
			} else {
				sql = "update sys_content  set content=? where id=?";
				dao.update(sql, new Object[] { syscontent.getContent(),
						syscontent.getId() });
			}

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public SysContent getComany() {
		String sql = "select * from sys_content where code=1  ";
		List<SysContent> list = dao.findList(sql);
		if (list != null && list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	public SysContent getConnection() {
		String sql = "select * from sys_content where code=99  ";
		List<SysContent> list = dao.findList(sql);
		if (list != null && list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	public boolean addDev(SysContent syscontent) {

		syscontent.setCreateDate(DateTimeUtil.parseDate(syscontent
				.getDateString()));
		String sql = "";

		try {
			if (syscontent.getId() == null) {
				sql = "insert into sys_content(title,code,content,createDate) values(?,?,?,?)";
				dao.insert(sql, new Object[] { syscontent.getTitle(),
						syscontent.getCode(), syscontent.getContent(),
						syscontent.getCreateDate() });
			} else {
				sql = "update sys_content  set title=?,code=?,content=?,createDate=? where id=?";
				dao.update(sql, new Object[] { syscontent.getTitle(),
						syscontent.getCode(), syscontent.getContent(),
						syscontent.getCreateDate(), syscontent.getId() });
			}
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	public List<SysContent> getDevContent() {
		String sql = "select * from sys_content where code=3 or  code=4  ";
		List<SysContent> list = dao.findList(sql);
		if (list != null && list.size() > 0) {
			List<SysContent> contentlist = new ArrayList<SysContent>();
			for (int i = 0; i < list.size(); i++) {
				SysContent content = list.get(i);
				content.setDateString(DateTimeUtil.switchDateToString(content
						.getCreateDate(), "yyyy年"));
				if (content.getContent() != null
						&& !"".equals(content.getContent())) {
					if (content.getContent().length() > 30)
						content.setContent(content.getContent().substring(30)
								+ "...");

				}
				contentlist.add(content);
			}
			return contentlist;
		}
		return null;
	}

	public List<SysContent> getPaperContent() {
		String sql = "select * from sys_content where code=10 or  code=11  ";
		List<SysContent> list = dao.findList(sql);
		if (list != null && list.size() > 0) {
			List<SysContent> contentlist = new ArrayList<SysContent>();
			for (int i = 0; i < list.size(); i++) {
				SysContent content = list.get(i);
				content.setDateString(DateTimeUtil.switchDateToString(content
						.getCreateDate(), "yyyy年"));

				contentlist.add(content);
			}
			return contentlist;
		}
		return null;
	}

	public List<SysContent> getCultureContent() {
		String sql = "select * from sys_content where code=14 or  code=15  ";
		List<SysContent> list = dao.findList(sql);
		if (list != null && list.size() > 0) {
			List<SysContent> contentlist = new ArrayList<SysContent>();
			for (int i = 0; i < list.size(); i++) {
				SysContent content = list.get(i);
				content.setDateString(DateTimeUtil.switchDateToString(content
						.getCreateDate(), "yyyy年"));

				contentlist.add(content);
			}
			return contentlist;
		}
		return null;
	}

	public List<SysContent> getNewContent() {
		String sql = "select * from sys_content where code=5 or  code=6 or code=7 ";
		List<SysContent> list = dao.findList(sql);
		if (list != null && list.size() > 0) {
			List<SysContent> newlist = new ArrayList<SysContent>();
			for (int i = 0; i < list.size(); i++) {
				SysContent content = list.get(i);
				if ("5".equals(content.getCode()))
					content.setCodeString("集团新闻");
				if ("6".equals(content.getCode()))
					content.setCodeString("区域公司新闻");
				if ("7".equals(content.getCode()))
					content.setCodeString("行业资讯");
				newlist.add(content);
			}
			return newlist;
		}

		return null;
	}

	public List<SysContent> getCustomContent() {
		String sql = "select * from sys_content where code=8 or  code=9 ";
		List<SysContent> list = dao.findList(sql);
		if (list != null && list.size() > 0)
			return list;
		return null;
	}

	public boolean deleteById(String idString) {
		boolean isFlag = true;
		if (idString == null || "".equals(idString))
			return false;
		String sql = "delete from sys_content where  id=?";
		String[] delteid = idString.split("_");
		try {
			for (int i = 0; i < delteid.length; i++) {
				sql = "delete from sys_content where  id=" + delteid[0];
				dao.delete(sql);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return isFlag;
	}

	public SysContent getById(String id) {
		String sql = "select * from sys_content where  id=?";
		List<SysContent> list = dao.findList(sql, new Object[] { id });
		if (list != null && list.size() > 0) {
			SysContent content = list.get(0);
			content.setDateString(DateTimeUtil.switchDateToString(content
					.getCreateDate(), "yyyy年"));
			return content;
		}
		return null;
	}

	public List<SysContent> getContent(int start, int limit, String code) {
		String sql = "select * from sys_content where code = ?";
		List<SysContent> list = dao.findList(dao.createSqlQuery(sql)
				.setFirstResult(start).setMaxSize(limit).setParameters(1,
						"'" + code + "'").bulidSql());
		return list;
	}

	public SysContent getContent(String type) {
		String sql = "select * from sys_content where code= '"+type+"'";
		List<SysContent> list = dao.findList(sql);
		if (list != null && list.size() > 0)
			return list.get(0);
		else
			return null;
	}

}
